package com.suneee.trades.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.trades.dao.TradesOrderDao;
import com.suneee.trades.entity.TradesOrder;

@Repository("tradesOrderDao")
public class TradesOrderDaoImpl extends GenericDao<TradesOrder, Integer>implements TradesOrderDao {

	@Override
	public TradesOrder getTradesOrder(TradesOrder dto) {
		WhereClauseBuilder sbf = this.wrapWhereSQL(dto);
		List<TradesOrder> list = this.selectWhere(sbf.getSql(), sbf.getArgs());
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	private WhereClauseBuilder wrapWhereSQL(TradesOrder dto) {
		WhereClauseBuilder sbf = new WhereClauseBuilder();
		if (dto.getTid() != null) {
			sbf.equals("tid", dto.getTid());
		}
		if (!StringUtils.isEmpty(dto.getOid())) {
			sbf.equals("oid", dto.getOid());
		}
		sbf.equals("outer_sku_id", dto.getOuterSkuId());
		sbf.equals("sku_id", dto.getSkuId());
		return sbf;
	}

	@Override
	public List<TradesOrder> batchTradesOrderByTids(String[] tids) {
		WhereClauseBuilder wcb = new WhereClauseBuilder(" select * from t_trades_order where 1=1 ");
		List<Object> list = new ArrayList<>();
		Collections.addAll(list, tids);
		wcb.in("tid", list);
		return this.executeQuery(wcb.getSql(), wcb.getArgs());
	}

	@Override
	public List<TradesOrder> getTradesOrderByTid(String tid) {
		WhereClauseBuilder sbf = new WhereClauseBuilder();
		sbf.equals("tid",tid);
		List<TradesOrder> od = selectWhere(sbf.getSql(),sbf.getArgs());
		return od;
	}

	@Override
	public String getOidsByOrderids(String[] order_ids) {
		WhereClauseBuilder wcb = new WhereClauseBuilder("select GROUP_CONCAT(oid) as oid from t_trades_order where 1=1 ");
		List<Object> args = new ArrayList<>();
		Collections.addAll(args, order_ids);
		wcb.in("order_id", args);
		List<TradesOrder> list = this.executeQuery(wcb.getSql(), wcb.getArgs());
		TradesOrder tradesOrder = list.get(0);
		return tradesOrder.getOid();
	}

	@Override
	public List<TradesOrder> getTradesOrderNotSpByTid(String tid) {
		WhereClauseBuilder sbf = new WhereClauseBuilder();
		sbf.equals("tid",tid);
		sbf.isNull("split_num");
		List<TradesOrder> od = selectWhere(sbf.getSql(),sbf.getArgs());
		return od;
	}

	@Override
	public TradesOrder getOrderDetail(TradesOrder dto) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = new WhereClauseBuilder();
		if (dto.getTid() != null) {
			sbf.equals("tid", dto.getTid());
		}
		if (!StringUtils.isEmpty(dto.getOid())) {
			sbf.equals("oid", dto.getOid());
		}
		sbf.equals("outer_sku_id", dto.getOuterSkuId());
		sbf.equals("sku_id", dto.getSkuId());
//		if(dto.getIsChanged() != null)
//			sbf.isNotNull("is_changed");
//		if(dto.getIsNew() != null)
//			sbf.isNotNull("is_new");
		List<TradesOrder> list = this.selectWhere(sbf.getSql(), sbf.getArgs());
		if (list != null && !list.isEmpty())
			return list.get(0);
		else
			return null;
	}

	@Override
	public List<TradesOrder> getNotSynTradesOrderByTid(String tid) {
		Object[] args = new Object[1];
		args[0] = tid;
//		args[1] = "";
		return this.selectWhere(" tid=? ", args);
	}
}
